﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net.NetworkInformation;
using System.Net;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;

namespace jNetServer
{
    public partial class Form1 : Form
    {
        private BaseWebServer webs = null;

        public Form1()
        {
            InitializeComponent();

            BinData();
        }

        private void BinData()
        {
            try
            {
                string nginxRoot = ConfigurationManager.AppSettings["nginxRoot"].ToString();
                string nginxConfTemp = ConfigurationManager.AppSettings["nginxConfTemp"].ToString();
                string root = ConfigurationManager.AppSettings["root"].ToString();
                string port = ConfigurationManager.AppSettings["port"].ToString();
                string autoPort = ConfigurationManager.AppSettings["autoPort"].ToString();
                string index = ConfigurationManager.AppSettings["index"].ToString();
                string server = ConfigurationManager.AppSettings["server"].ToString();

                if (server.ToLower() == "nginx")
                {
                    webs = new BaseWebServer(Application.StartupPath + @"\" + nginxRoot, Application.StartupPath + @"\" + nginxConfTemp, "../" + root, int.Parse(port), bool.Parse(autoPort), index);
                }
                else
                {
                    webs = new NetServer(Application.StartupPath, root, int.Parse(port), bool.Parse(autoPort), index);
                }

                if (webs.Start())
                {
                    开启服务ToolStripMenuItem.Text = "停止服务";
                    timer1.Enabled = true;
                }
                else
                {
                    开启服务ToolStripMenuItem.Text = "开启服务";

                    if (MessageBox.Show("服务没有启动", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
                    {
                        Application.Exit();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
                Application.Exit();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (webs != null)
                webs.OpenHome();
            timer1.Enabled = false;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (webs != null)
            {
                webs.Stop();
            }
        }

        private void 开启服务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (开启服务ToolStripMenuItem.Text == "停止服务")
            {
                开启服务ToolStripMenuItem.Text = "开启服务";
                if (webs != null)
                {
                    webs.Stop();
                }
            }
            else
            {
                开启服务ToolStripMenuItem.Text = "停止服务";
                BinData();
            }
        }

        private void 在浏览器中打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (开启服务ToolStripMenuItem.Text == "停止服务")
            {
                if (webs != null)
                {
                    webs.OpenHome();
                }
            }
            else
            {
                if (MessageBox.Show("服务没有启动!\n是否启动服务并打开浏览器？", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    BinData();
                }
            }
        }
    }
}
